//
//  AddressMangeMentViewController.m
//  何五路
//
//  Created by _ADY on 15/7/1.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "AddressMangeMentViewController.h"

@interface AddressMangeMentViewController ()

@end
#define AddMentHight 80
@implementation AddressMangeMentViewController
@synthesize AddressesArray,mTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"管理收货地址";
        self.view.backgroundColor = bgColor;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenMySize.size.width, screenMySize.size.height-60)];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.sectionHeaderHeight = 0.0;
    mTableView.sectionFooterHeight = 0.0;
    mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mTableView];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
    [mTableView setTableFooterView:v];
    
    [self myBarView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(treatedATask) name:KAddressUpdate object:nil];
}

-(void)treatedATask
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *ret2 = [SendService Addresses];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (AddressesArray !=nil) {
                [AddressesArray removeAllObjects];
                AddressesArray = nil;
            }
            AddressesArray = [[NSMutableArray alloc] init];
            
            for (NSDictionary *pic in [[ret2 objectForKey:@"Entity"] objectForKey:@"Addresses"])
            {
                [AddressesArray addObject:pic];
            }
            [self.delegate selectAddressAddressesArray:AddressesArray];
            [mTableView reloadData];
            
        });
    });
}

-(void)myBarView
{
    UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(0, screenMySize.size.height-60, screenMySize.size.width, 60)];
    barView.backgroundColor = [UIColor colorWithWhite:0 alpha:.8];
    [self.view addSubview:barView];

    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [addButton setFrame:CGRectMake(0, 0, screenMySize.size.width, barView.frame.size.height)];
    [addButton setBackgroundColor:[UIColor colorWithRed:238/255.0 green:84/255.0 blue:84/255.0 alpha:1]];
    [addButton setTitle:@"添加新地址" forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(settlementAction) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:addButton];
    
}
#pragma mark 添加新地址
-(void)settlementAction
{
    AddressAddViewController *details = [[AddressAddViewController alloc] init];
    details.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:details animated:YES];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return AddressesArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return AddMentHight;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"AddressMentCell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        aTableView.showsVerticalScrollIndicator = NO;
        cell.tintColor = [UIColor colorWithRed:243/255.0 green:125/255.0 blue:125/255.0 alpha:1];//勾选颜色
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    cell.accessoryType =  UITableViewCellAccessoryNone;
    cell.backgroundColor = [UIColor clearColor];
    
    NSDictionary * AddressesDic = [AddressesArray objectAtIndex:indexPath.row];
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(15,10, 100,20) setTitle:[NSString stringWithFormat:@"%@",[AddressesDic objectForKey:@"Name"]] setFont:[UIFont fontWithName:@"TrebuchetMS-Bold" size:labelSize] setColor:[UIColor blackColor]]];
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(135,10, screenMySize.size.width-150,20) setTitle:[NSString stringWithFormat:@"%@",[AddressesDic objectForKey:@"CellphoneNumber"]] setFont:[UIFont fontWithName:@"TrebuchetMS-Bold" size:labelSize] setColor:[UIColor blackColor]]];
    
    int WightMR = 0;
    if ([[NSString stringWithFormat:@"%@",[AddressesDic objectForKey:@"IsDefaultShippingAddress"]] isEqualToString:@"1"])
    {
        WightMR =40;
        cell.accessoryType =  UITableViewCellAccessoryCheckmark;
        cell.backgroundColor = [UIColor brownColor];
        
        [cell.contentView addSubview:[self setFrame:CGRectMake(20,35, WightMR,AddMentHight-45) setTitle:@"[默认]" setFont:[UIFont fontWithName:@"Arial" size:labelSize-2] setColor:[UIColor redColor]]];
        
    }
    [cell.contentView addSubview:[self setFrame:CGRectMake(20+WightMR,35, screenMySize.size.width-40-WightMR,AddMentHight-45) setTitle:[NSString stringWithFormat:@"%@%@%@%@%@",[AddressesDic objectForKey:@"StateProvinceName"],[AddressesDic objectForKey:@"CityName"],[AddressesDic objectForKey:@"DistrictName"],[AddressesDic objectForKey:@"StreetName"],[AddressesDic objectForKey:@"Address1"]] setFont:[UIFont fontWithName:@"Arial" size:labelSize-2] setColor:[UIColor blackColor]]];

    // Configure the cell...
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    ToviewAddressViewController *details = [[ToviewAddressViewController alloc] init];
    details.hidesBottomBarWhenPushed = YES;
    details.dic = [AddressesArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:details animated:YES];
}

-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title setFont:(UIFont*)font setColor:(UIColor*)myColor
{
    title = [title stringByReplacingOccurrencesOfString:@"<null>" withString:@""];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 0;
    titleLabel.numberOfLines = 0;
    titleLabel.font = font;
    titleLabel.textColor = myColor;
    return titleLabel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
